package com.saurshaz.testmvc.controllers;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.saurshaz.testmvc.service.NodeService;

@Controller
@RequestMapping(method = RequestMethod.GET, value = "/home")
public class HomeController extends AbstractController {

	@Autowired
	private NodeService nodeService;

	public NodeService getNodeService() {
		return nodeService;
	}

	public void setNodeService(NodeService nodeService) {
		this.nodeService = nodeService;
	}

	@RequestMapping("/nod.json")
	public @ResponseBody
	MyCollectionJsonObject getAll() {
		MyCollectionJsonObject myCollectionJsonObject = new MyCollectionJsonObject();
		System.out.println("Java code " + myCollectionJsonObject);
		return myCollectionJsonObject;
	}

	protected static void baseM() throws Exception {

	}

	@Override
	@RequestMapping("/listnodes")
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		PrintWriter out = response.getWriter();
		response.setContentType("application/json");

		// make service call to get entities as one object containing collection
		// data for the tree
		MyCollectionJsonObject myCollectionJsonObject = nodeService
				.getAllNodesAsObject();
		System.out.println("Java code " + myCollectionJsonObject);
		out.println(myCollectionJsonObject);
		out.close();

		// ModelAndView modelAndView = new ModelAndView();
		// modelAndView.setViewName("jsonView");
		// modelAndView.addObject("myCollectionJsonObject",
		// myCollectionJsonObject);

		return null;
	}
}

class X extends HomeController {

	X() {

	}

	public static void baseM() throws Exception {

		try {

		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
